package dfbz.com.controller.user;

import dfbz.com.controller.BaseServlet;
import dfbz.com.model.UserFocus;
import dfbz.com.service.UserService;
import dfbz.com.service.impl.UserServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();


    public void list(HttpServletRequest request, HttpServletResponse response) {
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            if (null != search) {
                byte[] bytes = search.getBytes("ISO-8859-1");
                search = new String(bytes, "UTF-8");
            }
            int page = 1;
            if (null != currentPage) {
                page = Integer.parseInt(currentPage);
            }
            if (null != search) {
                response.sendRedirect(request.getContextPath() + "/view/userList?page=" + page + "&search=" + search);
            } else {
                response.sendRedirect(request.getContextPath() + "/view/userList?page=" + page);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void unfollow(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("content-Type", "text/html");
            String ufId = request.getParameter("ufId");
            if (null != ufId) {
                userService.unFollow(Integer.parseInt(ufId));
                response.getWriter().write("success");
            } else {
                response.getWriter().write("请传ufId");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void follow(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setHeader("content-Type", "text/html");
            String userFocusId = request.getParameter("uId");
            if (null != userFocusId) {
                String userId = request.getSession().getAttribute("userId").toString();
                if (userId.equals(userFocusId)) {
                    //自己不能关注自己
                    response.getWriter().write("不能关注自己");
                } else {
                    //通过当前登录的用户Id 以及 要关注的用户ID(userFocusId) --> 然后去关注表查询有没有数据先
                    UserFocus focus = userService.findFollowData(Integer.parseInt(userId), Integer.parseInt(userFocusId));
                    if (null == focus) {
                        //没有数据接下来才可以添加关注信息
                        userService.followUser(Integer.parseInt(userId), Integer.parseInt(userFocusId));
                        response.getWriter().write("success");
                    } else {
                        response.getWriter().write("关注失败");
                    }
                }
            } else {
                response.getWriter().write("关注失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
